package com.guyu.common.core.entity;

import com.guyu.common.core.exception.BizException;
import com.guyu.common.core.exception.code.ErrorCodeEnum;
import com.guyu.common.core.exception.code.IErrorCode;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;


/**
 * 统一的业务返回包装类
 */
@Getter
@Accessors(chain = true)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class BizResult<T> implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 是否成功
     */
    @Setter(AccessLevel.PRIVATE)
    private boolean success = false;
    /**
     * 返回码
     */
    @Setter(AccessLevel.PRIVATE)
    private Integer code;
    /**
     * 返回码描述
     */
    @Setter(AccessLevel.PRIVATE)
    private String msg;
    /**
     * 业务码
     */
    @Setter(AccessLevel.PUBLIC)
    private String bizCode;

    /**
     * 正常请求的返回信息
     */
    @Setter(AccessLevel.PUBLIC)
    private T data;

    /**
     * 正常返回
     *
     * @param <T> 返回数据的类型
     * @return 包装后的返回
     */
    public static <T> BizResult<T> success() {
        return new BizResult<T>()
                .setSuccess(true)
                .setCode(ErrorCodeEnum.SUCCESS.getCode())
                .setMsg(ErrorCodeEnum.SUCCESS.getMsg());
    }

    /**
     * 正常返回
     *
     * @param data 数据内容
     * @param <T>  返回数据的类型
     * @return 包装后的返回
     */
    public static <T> BizResult<T> success(T data) {
        return new BizResult<T>()
                .setSuccess(true)
                .setCode(ErrorCodeEnum.SUCCESS.getCode())
                .setMsg(ErrorCodeEnum.SUCCESS.getMsg())
                .setData(data);
    }

    /**
     * 错误返回
     */
    public static <T> BizResult<T> error(Integer code, String msg) {
        return error(code, msg, null);
    }

    /**
     * 错误返回
     */
    public static <T> BizResult<T> error(Integer code, String msg, String bizCode) {
        return new BizResult<T>()
                .setSuccess(false)
                .setCode(code)
                .setMsg(msg)
                .setBizCode(bizCode);
    }

    /**
     * 错误返回
     */
    public static <T> BizResult<T> error(IErrorCode error) {
        return error(error.getCode(), error.getMsg(), error.getBizCode());
    }

    /**
     * 错误返回
     */
    public static <T> BizResult<T> error(IErrorCode error, String msg) {
        return error(error.getCode(), error.getMsg(), error.getBizCode());
    }

    /**
     * 错误返回
     */
    public static <T> BizResult<T> error(BizException ex) {
        return error(ex.getCode(), ex.getMsg(), ex.getBizCode());
    }

    /**
     * 弹框提示
     */
    public static <T> BizResult<T> hint(String msg) {
        return error(ErrorCodeEnum.HINT, msg);
    }

    /**
     * 弹框提示跳转
     */
    public static <T> BizResult<T> hintJump(String msg) {
        return error(ErrorCodeEnum.HINT_JUMP, msg);
    }

}
